/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package my.home.common.util; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.util.Base64; import android.util.Base64OutputStream; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.channels.FileChannel; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; /** * Created by legendmohe on 15/5/7. */ public class FileUtil { public static final String TAG = "FileUtil"; public static boolean isExternalStorageWritable() { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); } public static File getPictureStorageDir(String dirName) { File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), dirName); if (!file.exists() && !file.isDirectory() && file.mkdirs()) { Log.e(TAG, "Directory not created"); } return file; } public static void copy(File src, File dst) throws IOException { FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(src).getChannel(); destination = new FileOutputStream(dst).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } } public static String getUniquePrefix(String src, String suffix) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss", Locale.CHINA); String timeStamp = dateFormat.format(new Date()); return src + "_" + timeStamp + "." + suffix; } public static String getPathFromUri(Context context, Uri uri) { String filePath = null; if (uri != null && "content".equals(uri.getScheme())) { Cursor cursor = context.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null); if (cursor != null) { cursor.moveToFirst(); filePath = cursor.getString(0); cursor.close(); } } else { filePath = uri.getPath(); } return filePath; } public static String FileToBase64(File srcFile) { InputStream inputStream = null;//You can get an inputStream using any IO API try { inputStream = new FileInputStream(srcFile.getAbsolutePath()); } catch (FileNotFoundException e) { return null; } byte[] buffer = new byte[8192]; int bytesRead; ByteArrayOutputStream output = new ByteArrayOutputStream(); Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT); try { while ((bytesRead = inputStream.read(buffer)) != -1) { output64.write(buffer, 0, bytesRead); } } catch (Exception e) { return null; } try { output64.close(); } catch (IOException e) { return null; } return output.toString(); } public static boolean isExternalStorageRemovable() { return Environment.isExternalStorageRemovable(); } public static File getExternalCacheDir(Context context) { return context.getExternalCacheDir(); } public static String getDiskCacheDir(Context context, String uniqueName) { return getDiskCacheDir(context) + File.separator + uniqueName; } public static String getDiskCacheDir(Context context) { // Check if media is mounted or storage is built-in, if so, try and use external cache dir // otherwise use internal cache dir final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() : context.getCacheDir().getPath(); return cachePath; } }